﻿Imports System.ComponentModel
Imports System.Reflection
Imports System.Runtime.InteropServices



Public Class DriveMapper

#Region "Win32 Functions"

    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
        (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String,
         ByVal dwFlags As Integer) As Integer

    Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
      "WNetCancelConnection2A" (ByVal lpName As String, _
      ByVal dwFlags As Long, ByVal fForce As Integer) As Long

#End Region

#Region "Constants"

    Public Const ForceDisconnect As Integer = 1
    Public Const CONNECT_UPDATE_PROFILE = &H1

    ' The following includes all the constants defined for NETRESOURCE,
    ' not just the ones used in this example.
    Public Const RESOURCETYPE_DISK = &H1
    Public Const RESOURCETYPE_PRINT = &H2
    Public Const RESOURCETYPE_ANY = &H0
    Public Const RESOURCE_CONNECTED = &H1
    Public Const RESOURCE_REMEMBERED = &H3
    Public Const RESOURCE_GLOBALNET = &H2
    Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
    Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
    Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
    Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
    Public Const RESOURCEUSAGE_CONNECTABLE = &H1
    Public Const RESOURCEUSAGE_CONTAINER = &H2
    ' Error Constants:
    Public Const NO_ERROR = 0
    Public Const ERROR_BAD_NETPATH = 53&
    Public Const ERROR_NETWORK_ACCESS_DENIED = 65&
    Public Const ERROR_NETWORK_BUSY = 54&
    Public Const ERROR_ACCESS_DENIED = 5&
    Public Const ERROR_ALREADY_ASSIGNED = 85&
    Public Const ERROR_BAD_DEV_TYPE = 66&
    Public Const ERROR_BAD_DEVICE = 1200&
    Public Const ERROR_BAD_NET_NAME = 67&
    Public Const ERROR_BAD_PROFILE = 1206&
    Public Const ERROR_BAD_PROVIDER = 1204&
    Public Const ERROR_BUSY = 170&
    Public Const ERROR_CANCELLED = 1223&
    Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
    Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
    Public Const ERROR_EXTENDED_ERROR = 1208&
    Public Const ERROR_INVALID_PASSWORD = 86&
    Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&

#End Region

#Region "Structures"
    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure
#End Region

#Region "Properties"

    Private _drive As String
    Public Property DriveLetter() As String
        Get
            Return _drive
        End Get
        Set(ByVal value As String)
            _drive = value
        End Set
    End Property

    Private _uncpath As String
    Public Property UNCPath() As String
        Get
            Return _uncpath
        End Get
        Set(ByVal value As String)
            _uncpath = value
        End Set
    End Property

    Private _un As String = Nothing
    Public Property UserName() As String
        Get
            Return _un
        End Get
        Set(ByVal value As String)
            _un = value
        End Set
    End Property

    Private _unpw As String = Nothing
    Public Property UNPassword() As String
        Get
            Return _unpw
        End Get
        Set(ByVal value As String)
            _unpw = value
        End Set
    End Property

    Private _adminpw As String = Nothing
    Public Property AdminPassword() As String
        Get
            Return _adminpw
        End Get
        Set(ByVal value As String)
            _adminpw = value
        End Set
    End Property

#End Region

    Public Function MapDrive() As Long
        Dim nr As NETRESOURCE
        Dim Username As String = _un
        Dim Password As String = _unpw
        Dim DriveLetter As String = _drive
        Dim UNCPath As String = _uncpath

        Dim ErrInfo As Long

        nr = New NETRESOURCE
        nr.lpRemoteName = UNCPath
        nr.lpLocalName = DriveLetter & ":"
        ' Username = Nothing '(add parameters to pass this if necessary)
        ' Password = Nothing '(add parameters to pass this if necessary)
        nr.dwType = RESOURCETYPE_DISK

        'ErrInfo = WNetAddConnection2(nr, Password, Username, 0)
        ErrInfo = WNetAddConnection2(nr, Password, Username, 1)
        'ErrInfo = WNetAddConnection2(nr, Password, Username, Convert.ToInt32("8", 16))

        Return ErrInfo

    End Function

    Public Function UnMapDrive() As Long
        Dim DriveLetter As String = _drive

        Dim ErrInfo As Long
        Dim strLocalName As String = DriveLetter & ":"

        ' You may specify either the lpRemoteName or lpLocalName
        ' strLocalName = "\\ServerName\ShareName"
        ' strLocalName = DriveLetter & ":"
        ErrInfo = WNetCancelConnection2(strLocalName, CONNECT_UPDATE_PROFILE, ForceDisconnect)

        Return ErrInfo

    End Function

    'Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, ByVal strUsername As String, ByVal strPassword As String) As Boolean


    '    Dim nr As NETRESOURCE

    '    nr = New NETRESOURCE
    '    nr.lpRemoteName = UNCPath
    '    nr.lpLocalName = DriveLetter & ":"
    '    nr.lpProvider = Nothing
    '    nr.dwType = RESOURCETYPE_DISK

    '    Dim result As Integer
    '    result = WNetAddConnection2(nr, strPassword, strUsername, 0)

    '    If result = 0 Then
    '        Return True
    '    Else
    '        Select Case result
    '            Case ERROR_BAD_NETPATH
    '                statusBarMsg("QA4001I", "Bad path could not connect to Star Directory")
    '            Case ERROR_INVALID_PASSWORD
    '                statusBarMsg("QA4002I", "Invalid password could not connect to Star Directory")
    '            Case ERROR_NETWORK_ACCESS_DENIED
    '                statusBarMsg("QA4003I", "Network access denied could not connect to Star Directory")
    '            Case ERROR_NETWORK_BUSY
    '                statusBarMsg("QA4004I", "Network busy could not connect to Star Directory")
    '        End Select
    '        Return False
    '    End If

    'End Function




End Class


